package com.example.yls.demoa;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private Button button6;
    private Button button7;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);
        button5 = findViewById(R.id.button5);
        button6 = findViewById(R.id.button6);
        button7 = findViewById(R.id.button7);
        button6.setOnClickListener(this);
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
                // 正在加载
                dialog.setTitle("正在加载");

                dialog.setIcon(android.R.drawable.ic_menu_more);
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 主题
                dialog.setMax(100);// 最大值

                // 关闭'按钮点界面的自动取消'功能
                dialog.setCancelable(false);
                dialog.show();
                // 进度条效果
                Thread th = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < 100; i++) {
                            dialog.setProgress(i);
                            try {
                                // 休眠
                                Thread.sleep(80);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        // 关闭当前对话框
                        dialog.dismiss();
                    }
                });
                th.start();

            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.ic_launcher_background);
                builder.setTitle("弹出对话框");
                builder.setMessage("确定删除吗?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        Toast.makeText(MainActivity.this, "positive:" + which, Toast.LENGTH_SHORT).show();

                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int negative) {
                        Toast.makeText(MainActivity.this, "positive:" + negative, Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        Toast.makeText(MainActivity.this, "neutral:" + which, Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();

            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.ic_launcher_background);
                builder.setTitle("选择一个城市");
                final String[] cities = {"广州", "深圳", "东莞", "茂名", "高州"};
                builder.setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        Toast.makeText(MainActivity.this, "选择的城市:" + cities[which], Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.ic_launcher_background);
                builder.setTitle("选择性别");
                final String[] sex = {"男", "女"};

                builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        Toast.makeText(MainActivity.this, "选择的性别:" + sex[which], Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.show();
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.ic_launcher_background);
                builder.setTitle("爱好");
                final String[] hobbies = {"篮球", "足球", "网球", "排球"};
                final boolean[] isCheck = new boolean[5];
                builder.setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener() {
                    StringBuffer stringBuffer = new StringBuffer(100);

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean isChecked) {
                        isCheck[i] = isChecked;
                        if (isChecked) {
                            Toast.makeText(MainActivity.this, "你选择了:" + hobbies[i], Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "你取消了:" + hobbies[i], Toast.LENGTH_SHORT).show();
                        }

                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        StringBuffer stringBuffer = new StringBuffer();
                        for (int m = 0; m < isCheck.length; m++) {
                            if (isCheck[m]) {
                                stringBuffer.append(hobbies[m]);
                            }
                        }
                        Toast.makeText(MainActivity.this, "你选择了:" + stringBuffer.toString(), Toast.LENGTH_SHORT).show();

                    }
                });
                builder.create();
                builder.show();

            }

        });

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.ic_launcher_background);
                builder.setTitle("请输入用户名和密码");
                //    通过LayoutInflater来加载一个xml的布局文件作为一个View对象
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog2, null);
                //    设置我们自己定义的布局文件作为弹出框的Content
                builder.setView(view);

                final EditText username = (EditText) view.findViewById(R.id.username);
                final EditText password = (EditText) view.findViewById(R.id.password);

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog2, int which) {
                        String a = username.getText().toString().trim();
                        String b = password.getText().toString().trim();
                        //    将输入的用户名和密码打印出来
                        Toast.makeText(MainActivity.this, "用户名: " + a + ", 密码: " + b, Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog2, int which) {

                    }
                });
                builder.show();
            }
        });
    }


    @Override
    public void onClick(View view) {

        AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.drawable.ic_launcher_background);
        builder.setTitle("请输入");

        //放进去任意简单的view
        builder.setView(new EditText(this));
        builder.setPositiveButton("确定", null);
        builder.setNegativeButton("取消", null);

        builder.create().show();
    }
}
